package com.itheima.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 短信工具类
 */
public class SMSUtils {

    private static final String regionId = "cn-hangzhou";
    private static final String signName = "阿里云短信测试"; //短信签名
    private static final String templateCode = "SMS_154950909"; //短信模板
    private static final String accessKeyId = "xxx xxx xxx";
    private static final String secret = "xxx xxx xxx";

    //发送短信
    public static void sendMessage(String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone); //手机号
        request.setSignName(signName); //短信签名
        request.setTemplateCode(templateCode); //短信模板
        request.setTemplateParam("{\"code\": " + code + "}"); //验证码不能带字母
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}
